<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>require_role (RoleRequirement::RoleSecurityClassMethods)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File lib/role_requirement_system.rb, line 32</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">require_role</span>(<span class="ruby-identifier">roles</span>, <span class="ruby-identifier">options</span> = {})
      <span class="ruby-identifier">options</span>.<span class="ruby-identifier">assert_valid_keys</span>(<span class="ruby-identifier">:if</span>, <span class="ruby-identifier">:only</span>, <span class="ruby-identifier">:except</span>, <span class="ruby-identifier">:unless</span>)
      
      <span class="ruby-comment cmt"># only declare that before filter once</span>
      <span class="ruby-keyword kw">unless</span> (<span class="ruby-ivar">@before_filter_declared</span><span class="ruby-operator">||=</span><span class="ruby-keyword kw">false</span>)
        <span class="ruby-ivar">@before_filter_declared</span>=<span class="ruby-keyword kw">true</span>
        <span class="ruby-identifier">before_filter</span> <span class="ruby-identifier">:check_roles</span>
      <span class="ruby-keyword kw">end</span>
      
      <span class="ruby-comment cmt"># convert to an array if it isn't already</span>
      <span class="ruby-identifier">roles</span> = [<span class="ruby-identifier">roles</span>] <span class="ruby-keyword kw">unless</span> <span class="ruby-constant">Array</span><span class="ruby-operator">===</span><span class="ruby-identifier">roles</span>
      
      <span class="ruby-comment cmt"># convert any actions into symbols</span>
      <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">key</span> <span class="ruby-keyword kw">in</span> [<span class="ruby-identifier">:only</span>, <span class="ruby-identifier">:except</span>]
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">key</span>)
          <span class="ruby-identifier">options</span>[<span class="ruby-identifier">key</span>] = [<span class="ruby-identifier">options</span>[<span class="ruby-identifier">key</span>]] <span class="ruby-keyword kw">unless</span> <span class="ruby-constant">Array</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">key</span>]
          <span class="ruby-identifier">options</span>[<span class="ruby-identifier">key</span>] = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">key</span>].<span class="ruby-identifier">collect</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">v</span>.<span class="ruby-identifier">to_sym</span>}
        <span class="ruby-keyword kw">end</span> 
      <span class="ruby-keyword kw">end</span>
      
      <span class="ruby-ivar">@role_requirements</span><span class="ruby-operator">||=</span>[]
      <span class="ruby-ivar">@role_requirements</span> <span class="ruby-operator">&lt;&lt;</span> {<span class="ruby-identifier">:roles</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">roles</span>, <span class="ruby-identifier">:options</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span> }
    <span class="ruby-keyword kw">end</span></pre>
</body>
</html>